﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CSharp.Libs.Database;
using CSharp.Libs.Media;

namespace CSharp.Libs.WPF.UserControls
{
    public class FileTaggerListViewViewModel : ViewModelBase
    {
        #region declarations
        #endregion



        #region constructor
        public FileTaggerListViewViewModel()
        {
        }
        #endregion



        #region private functions
        private void _readTargetDir()
        {
            _ocListItems.Clear();

            string[] strSubDirs;
            string[] strFiles;

            try
            {
                strSubDirs = Directory.GetDirectories(_strTargetDir);
                strFiles = Directory.GetFiles(_strTargetDir);

                foreach (string strSubDir in strSubDirs)
                {
                    FileTaggerListViewItem ftlvi = new FileTaggerListViewItem();
                    ftlvi.FileName = new DirectoryInfo(strSubDir).Name;
                    ftlvi.FullFileName = strSubDir;
                    _ocListItems.Add(ftlvi);
                }

                foreach (string strFile in strFiles)
                {
                    FileTaggerListViewItem ftlvi = new FileTaggerListViewItem();
                    ftlvi.FileName = Path.GetFileName(strFile);
                    ftlvi.FullFileName = strFile;
                    _ocListItems.Add(ftlvi);
                }

                using (DirectoryLister dl = new DirectoryLister())
                {
                    dl.LoadFileItemsAsync(_ocListItems);
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void _addTag()
        {
            if (!string.IsNullOrEmpty(_strTagInput) &&
                _ftlviSelectedItem != null)
            {
                //prepare FileTagger Class
                using (FileTagger ft = new FileTagger()
                {
                    Storage = new DataAccessLayer(new SQLiteHandler(
                        string.Concat(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "\\FileTagger.s3db")))
                })
                {
                    //add tag to fileobject
                    ft.AddTagToFileObject(ft.GetFileObjectForFile(_ftlviSelectedItem.FullFileName), new TagObject() { Tag = _strTagInput });

                    //finally clear Inputfield
                    _strTagInput = "";
                    OnPropertyChanged("TagInput");
                }
            }
        }
        #endregion



        #region properties
        private string _strTargetDir = "";
        public string TargetDirectory
        {
            get
            {
                return _strTargetDir;
            }
            set
            {
                if (_strTargetDir != value)
                {
                    _strTargetDir = value;
                    _readTargetDir();
                    OnPropertyChanged("TargetDirectory");
                    OnPropertyChanged("ListItems");
                }
            }
        }

        private ObservableCollection<FileTaggerListViewItem> _ocListItems = new ObservableCollection<FileTaggerListViewItem>();
        public ObservableCollection<FileTaggerListViewItem> ListItems { get { return _ocListItems; } }

        private FileTaggerListViewItem _ftlviSelectedItem;
        public FileTaggerListViewItem SelectedItem
        {
            get { return _ftlviSelectedItem; }
            set
            {
                _ftlviSelectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }

        private string _strTagInput = "";
        public string TagInput
        {
            get { return _strTagInput; }
            set
            {
                _strTagInput = value;
                OnPropertyChanged("TagInput");
            }
        }

        private RelayCommand _rcAddTag;
        public ICommand AddTag
        {
            get
            {
                if (_rcAddTag == null)
                {
                    _rcAddTag = new RelayCommand(
                        param => this._addTag(),
                        param => this.CanAddTag);
                }
                return _rcAddTag;
            }
        }
        public Boolean CanAddTag
        {
            get
            {
                //TODO check what there may be to check
                return true;
            }
        }

        #endregion



        #region methods
        public void AddExample()
        {
            //generate examples
            _ocListItems.Add(new FileTaggerListViewItem() { FileName = "Test.txt", FullFileName = "C:\\Test.txt", SeparatedTags = "Test|TextFile|notepad++" });

            OnPropertyChanged("ListItems");
        }
        #endregion
    }
}
